package com.situ.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


import com.situ.dao.IvArchiveDao;
import com.situ.dao.IvIninvoiceDao;
import com.situ.dao.IvOutinvoiceDao;
import com.situ.entity.IvIninvoice;
import com.situ.entity.IvOutinvoice;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.situ.service.IvArchiveService;
import com.situ.entity.IvArchive;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

/**
 * IvArchive的路由接口服务
 *
 * @author 胡文
 */
@Controller
public class IvArchiveController {


    /**
     * IvArchiveService服务
     */
    @Autowired
    private IvArchiveService ivArchiveService;
    @Autowired(required = false)
    private IvIninvoiceDao ivIninvoiceDao;
    @Autowired(required = false)
    private IvOutinvoiceDao ivOutinvoiceDao;
    @Autowired(required = false)
    private IvArchiveDao ivArchiveDao;





	//查询发票的方法
	@RequestMapping("inv_invoiceReportArchive.html")
	public String queryfapiao(Map map,@RequestParam(defaultValue ="1",required = false) int startPage,
							          @RequestParam(defaultValue = "5",required = false) int PageSize){
		PageHelper.startPage(startPage, PageSize);

		List<Map> list=ivArchiveService.queryfapiao();

		PageInfo pageInfo = new PageInfo<Map>(list,5);


		map.put("pageInfo", pageInfo);
		map.put("startPage", startPage);
		map.put("PageSize", PageSize);
		map.put("list", list);
		return "page/inv_invoiceReportArchive.html";
	}
	
	
	
	/**
	 * 通过id查询IvArchive数据的方法
	 * 
	 * @return
	 */

    /**
     * IvArchiveService服务
     */
    
   

    /**
     * 查询所有IvArchive数据的方法
     *
     * @param value
     * @return
     */
    @RequestMapping(value = "queryIvArchive", produces = {"application/json;charset=UTF-8"})
    public String find(IvArchive value, Map map) {
        List<IvArchive> list = ivArchiveService.find(value);
        map.put("items", list);

        return "页面名称自己改";
    }

    @RequestMapping("addArchive")
    public ModelAndView addArchive(Model modle) {
        modle.addAttribute("date", new Date());
        return new ModelAndView("inv_invoiceArchive.html");
    }

    @RequestMapping("ifecxistAdd")
    @ResponseBody
    public void ifecxist(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        String startno = request.getParameter("StartNo");
        String endno = request.getParameter("EndNo");
        IvIninvoice w = ivIninvoiceDao.Archive(startno, endno);

        PrintWriter out = response.getWriter();
        String counts = request.getParameter("Counts");
        System.out.println(counts);
        if (w == null) {

            out.write("true");
        } else {
            IvArchive ivout = new IvArchive();
            ivout.setArchiveDate(new Date());
            ivout.setStartNo(startno);
            ivout.setEndNo(endno);
            ivout.setCounts(Integer.parseInt(counts));
            int i = ivArchiveDao.insertIvArchive(ivout);
            if (i != 0) {
                System.out.println("成功");
            } else {
                System.out.println("失败");
            }
            Integer iv = w.getInvoiceType();

            if (iv == 0) {
                out.write("普通发票");
            }

            if (iv == 1) {
                out.write("普通发票");
            }
            if (iv == 2) {

                out.write("增值税发票");
            }
            System.out.println("false");

        }
    }

    @RequestMapping("queryIvArchive")
    @ResponseBody
    public void out(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        String startno = request.getParameter("StartNo");
        String endno = request.getParameter("EndNo");
        IvIninvoice w = ivIninvoiceDao.Archive(startno, endno);
        PrintWriter out = response.getWriter();
        if (w == null) {
            out.write("true");
        } else {
            Integer iv = w.getInvoiceType();
            if (iv == 0) {
                out.write("普通发票");
            }
            if (iv == 1) {
                out.write("增值税发票");
            }
            System.out.println("false");

        }
    }

    /**
     * 通过id查询IvArchive数据的方法
     *
     * @return
     */
    @RequestMapping(value = "queryIvArchiveById/{id}", produces = {"application/json;charset=UTF-8"})
    public String findOne(@PathVariable(name = "id") Object id, Map map) {
        IvArchive obj = ivArchiveService.findOne(id);
        map.put("IvArchive", obj);
        return "页面名称自己改";
    }

    /**
     * 插入IvArchive属性不为空的数据方法
     *
     * @return
     */
    @RequestMapping(value = "saveIvArchive", produces = {"application/json;charset=UTF-8"})
    public String save(IvArchive value) {
        ivArchiveService.saveNotNull(value);

        return "redirect:/queryIvArchive";
    }


    /**
     * 更新IvArchive属性不为空的数据方法
     *
     * @return
     */
    @RequestMapping(value = "updateIvArchive", produces = {"application/json;charset=UTF-8"})
    public String update(IvArchive value) {
        ivArchiveService.updateNotNullById(value);
        return "redirect:/queryIvArchive";
    }

    /**
     * 通过id删除IvArchive数据方法
     *
     * @return
     */
    @RequestMapping(value = "delIvArchiveById/{id}", produces = {"application/json;charset=UTF-8"})
    public String delete(@PathVariable(name = "id") Object id) {
        ivArchiveService.deleteById(id);
        return "redirect:/queryIvArchive";
    }

}
